package com.qinglei.recoup.app.task;


import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qinglei.recoup.app.dao.RadarWechatAppMapper;
import com.qinglei.recoup.app.dao.RadarWechatAppSubscriptionMapper;
import com.qinglei.recoup.app.domain.RadarWechatApp;
import com.qinglei.recoup.app.domain.RadarWechatAppSubscription;
import com.qinglei.recoup.app.service.WxRadarService;
import com.qinglei.recoup.system.domain.RadarWechat;
import com.qinglei.recoup.system.domain.UserWechat;
import com.qinglei.recoup.system.service.RadarWechatService;
import com.qinglei.recoup.system.service.UserWechatService;
import com.qinglei.recoup.system.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Slf4j
@Component
public class WXTaskV1 {



    @Value("${wx.appId}")
    private  String APPID ;

    @Value("${wx.appSecret}")
    private  String SECRET;

    @Value("${wx.threadCount}")
    private  String threadCount;
    @Autowired
    private WxService wxService;
    @Autowired
    private WxRadarService wxRadarService;

    @Autowired
    private RadarWechatAppSubscriptionMapper radarWechatAppSubscriptionMapper;

    @Autowired
    private RadarWechatAppMapper radarWechatAppMapper;

    //t_radar_wechat_app
    private Executor executors = Executors.newFixedThreadPool(2);


    public void sendReport(){
        //t_radar_wechat_app_subscription
        log.info("____________________startsendReportXcx___________________________");
        QueryWrapper queryRadarWechatAppSubscription = new QueryWrapper();

        queryRadarWechatAppSubscription.eq("is_deleted","0") ;
        queryRadarWechatAppSubscription.eq("type","2");
        List<RadarWechatAppSubscription> radarWechatAppSubscriptionList =  radarWechatAppSubscriptionMapper.selectList(queryRadarWechatAppSubscription);
        for(RadarWechatAppSubscription radarWechatAppSubscription : radarWechatAppSubscriptionList){
               try {
                   QueryWrapper queryWrapper = new QueryWrapper();
                   queryWrapper.eq("open_id",radarWechatAppSubscription.getOpenId());
                   queryWrapper.eq("is_deleted","0") ;
                   queryWrapper.eq("sn_code",radarWechatAppSubscription.getSnCode());
                   RadarWechatApp radarWechatApp = radarWechatAppMapper.selectOne(queryWrapper);

                   String openId =  radarWechatAppSubscription.getOpenId();
                   String snCode =radarWechatAppSubscription.getSnCode();
                   String nickName = radarWechatApp.getTag();
                   CompletableFuture.runAsync(() -> {
                       try {
                           sendMessage(openId,snCode,nickName);
                       } catch (Exception e) {
                           log.error("sendMessage wx error", e);
                       }
                   }, executors);
               }catch (Exception exception){
                   log.error(exception.getMessage());
               }

        }
        log.info("____________________endsendWxMessage___________________________");
    }

    private void sendMessage(String openId,String snCode,String nickName){
        wxService.send_template_message(APPID,openId,snCode,nickName);
        wxService.sendTemplateMessageFirstDayOfMonth(APPID,openId,snCode,nickName);
        wxService.sendTemplateMessageFirstDayOfWeek(APPID,openId,snCode,nickName);
    }


}
